package com.sanbuTech.foundation.service.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.sanbuTech.foundation.exception.SanbuFoudationException;
import com.sanbuTech.foundation.manager.IdTableManager;
import com.sanbuTech.foundation.service.IIdService;
import com.sanbuTech.util.DateUtil;

@Service("idService")
public class IdServiceImpl implements IIdService {

	private static Logger logger = LoggerFactory.getLogger(IdServiceImpl.class); 
	
	@Autowired
	@Qualifier("idTableManager")
	private IdTableManager idTableManager; 
	
	/**
	 * 返回一个32位的id字符串
	 * @param appId	数字形式, 业务平台id5位, 位数不足高位以0补齐
	 * @param businessCode	数字形式, 业务动作6位, 位数不足高位以0补齐
	 * @return 业务平台id5位|时间戳10位|业务动作6位|id串11位
	 * @throws SanbuFoudationException
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public String getId(int appCode, int appBehave) throws SanbuFoudationException{
		//返回一个拼接的id, 32位
//		12345_6789012345_678901_23456789012
//		00001_1453812480_000011_00000000001
//		业务平台id5位|时间戳10位|业务动作6位|id串11位
		
		if(appCode <= 0 || appBehave <= 0){
			throw new SanbuFoudationException("param should greater than 0.");
		}
		
		int codeLen = 5;
		int behaveLen = 6;
		int increaseIdLen = 11;
		StringBuilder sb = new StringBuilder();
		
		String codeStr = String.valueOf(appCode);
		String behaveStr = String.valueOf(appBehave);
		
		if(codeStr.length() > codeLen){
			//抛出异常, 业务平台appId长度不能超过5位
			throw new SanbuFoudationException("length of appId should not be larger than 5.");
		}
		
		if(behaveStr.length() > behaveLen){
			//抛出异常, 业务类型码businessCode长度不能超过6位
			throw new SanbuFoudationException("length of businessCode should not be larger than 5.");
		}
		
		sb.append(String.format("%0"+codeLen+"d", appCode))			//业务平台id5位
			.append(new Date().getTime()/1000)					//时间戳10位
			.append(String.format("%0"+behaveLen+"d", appBehave));	//业务动作6位
		
		long id = idTableManager.getAvailableId();
		
		sb.append(String.format("%0"+increaseIdLen+"d", Long.valueOf(id)));
		
		return sb.toString();
	}
	
	/**
	 * 返回>=8位的可用id串
	 * @param length id串长度. length>=8
	 * @return
	 * @throws SanbuFoudationException
	 */
	public String getId(int length) throws SanbuFoudationException{
		StringBuilder sb = new StringBuilder();
		
		if(length <= 8){
			throw new SanbuFoudationException("长度必须大于等于8");
		}
		
		long id = idTableManager.getAvailableId();
		sb.append(String.format("%0"+length+"d", Long.valueOf(id)));
		
		return sb.toString();
	}
	
	/**
	 * 返回一个18位的id字符串
	 * 10位时间戳+8位id
	 * @return
	 * @throws SanbuFoudationException
	 */
	public String getShortId() throws SanbuFoudationException{
		StringBuilder sb = new StringBuilder();
		
		int increaseIdLen = 8;
		sb.append(new Date().getTime()/1000);					//时间戳10位
		
		long id = idTableManager.getAvailableId();
		
		sb.append(String.format("%0"+increaseIdLen+"d", Long.valueOf(id)));
		
		return sb.toString();
	}
	
}
